<?php


namespace app\admin\model;
use think\Model;

//客户模型
class projectKf extends Model
{

    public static function paramsNew() :array {
        return array(
            'kf_id' => '',
            'project_id' => '',
            'merchant_id' => 0,
            'created_at' => time(),
            'status' => 1,
        );
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_kf';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['kf_id', 'project_id'], 'required'],
            [['kf_id', 'project_id', 'created_at','merchant_id'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => '商户 ID',
            'kf_id' => '顾问 ID',
            'project_id' => '项目 ID',
            'created_at' => '创建时间',
        ];
    }
    /**
     * 关联会员
     */
    public function getKf()
    {
        return $this->hasOne(XcxKf::class, ['id' => 'kf_id']);
    }

    /**
     * 关联项目
     *
     * @return \yii\db\ActiveQuery
     */
    public function getProject()
    {
        return $this->hasMany(Project::class,['id' => 'project_id']);
    }


    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
